﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using KMS.Core.domain.model.alert;
using Moq;
using KMS.Web.Controllers;
using System.Web.Mvc;
using KMS.Core.application;
using KMS.Web.Views.News;
using KMS.Web.Tests.Factory;
using KMS.Core.domain.model.user;
using KMS.Core.domain.model.project;

namespace KMS.Web.Tests.Controllers
{
    /// <summary>
    /// Summary description for NewsControllerTests
    /// </summary>
    [TestClass]
    public class NewsControllerTests
    {
        private SystemAlertFactory _systemAlertFactory;
        private PostFactory _postFactory;
        private PostAlertFactory _postAlertFactory;

        public NewsControllerTests()
        {
            _systemAlertFactory = new SystemAlertFactory();
            _postFactory = new PostFactory();
            _postAlertFactory = new PostAlertFactory();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void ListActionReturnsNewsViewModelsFromService()
        {
            var resource = _postFactory.CreateResourceWithContent("my resource content");
            var news = new List<Alert> { _systemAlertFactory.Create("event 1"), _postAlertFactory.Create(resource), _systemAlertFactory.Create("event 3") };
            var newServiceMock = new Mock<INewsService>();
            newServiceMock.Setup(s => s.FindNewsForCurrentUser()).Returns(news);

            var controller = new NewsController(newServiceMock.Object, 10);

            var result = (ViewResult)controller.List();
            var newsList = (IList<AlertViewModel>)result.ViewData.Model;
            //var newsList = newsCollection.ToList();

            Assert.AreEqual(3, newsList.Count, "Result list should have 3 objects");

            var alert0 = newsList[0] as SystemAlertViewModel;
            Assert.IsNotNull(alert0, "newsList[0] should be SystemAlertViewModel");
            Assert.AreEqual("event 1", alert0.DisplayedText, "wrong newsList[0] text");

            var alert1 = newsList[1] as ResourceAlertViewModel;
            Assert.IsNotNull(alert1, "newsList[1] should be PostAlertViewModel");
            Assert.AreEqual("my resource content", alert1.Content);

            var alert2 = newsList[2] as SystemAlertViewModel;
            Assert.IsNotNull(alert2, "newsList[2] should be SystemAlertViewModel");
            Assert.AreEqual("event 3", alert2.DisplayedText, "wrong newsList[2] text");
        }
    }
}
